/**
 * Definition for singly-linked list.
 * function ListNode(val) {
 *     this.val = val;
 *     this.next = null;
 * }
 */

/**
 * 反转链表
 * 迭代
 * @param {*} head 
 * @returns 
 */
var reverseList = function (head) {
  var pre = null; // 前指针节点

  while (head) {
    var next = head.next; // 临时节点，暂存当前节点的下一节点，用于后移
    head.next = pre; // 将当前节点指向它前面的节点
    pre = head; // 前指针后移
    head = next; // 当前指针后移
  }

  return pre;
};

/**
 * 递归
 */
var reverseList = function (head) {
  if (!head || !head.next) {
    return head
  }
  let last = reverseList(head.next)
  head.next.next = head
  head.next = null
  return last
};

/**
 * 两两交换
 * 1234 -> 2143
 */
var swapPairs = function (head) {
  // let ret = new ListNode(0, head), temp = ret;
  while (head.next && head.next.next) {
    let cur = head.next.next, pre = head.next;
    pre.next = cur.next;
    cur.next = pre;
    head.next = cur;
    head = pre;
  }
  return head.next;
};